﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Truextend.CoffeShop.Bussiness;
using Truextend.CoffeShop.Model;
using System.Data;
using System.Collections;
using Truextend.CoffeShop.Service;

namespace Truextend.CoffeShop.Presenter.Orders
{
    public class ParticipateInOrderPresenter
    {
        IParticipateInOrder view;

        public ParticipateInOrderPresenter(IParticipateInOrder view) {
            this.view = view;
        }


        public void LoadOrder(string action)
        {
            OrderService orderService = new OrderService();
            RestaurantService restaurantService = new RestaurantService();

            Order order = orderService.GetOrderById(view.orderId);
            Restaurant restaurant = restaurantService.GetRestaurantByOrderId(view.orderId);

            view.title = order.Title;
            view.restaurant = restaurant.Name;

            if (action == "Join")
            {
                FillOptions(view.orderId);
            }
            else
            {
                FillEditOptions();
            }

        }

        private void FillEditOptions()
        {
            DetailService detailService = new DetailService();
            ConsumptionService consumptionService = new ConsumptionService();

            IList<Detail> listDetails = detailService.GetAllDetailsOfAnOrder(view.orderId);

            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("OptionID");
            dataTable.Columns.Add("CheckBoxOption", typeof(bool));
            dataTable.Columns.Add("AmountState", typeof (bool));
            dataTable.Columns.Add("Description");
            dataTable.Columns.Add("Price");
            dataTable.Columns.Add("Amount");

            float totalPrice = 0;

            foreach (var detail in listDetails)
            {

                Consumption consumption = consumptionService.GetConsumptionForDetailAndUserID(detail.DetailId,view.user_id);

                DataRow row = dataTable.NewRow();
                row["OptionID"] = detail.DetailId;
                row["Price"] = detail.Product.Price;
                row["Description"] = detail.Product.Name;

                if(consumption != null)
                {
                    row["CheckBoxOption"] = true;                    
                    row["Amount"] = consumption.Quantity;
                    row["AmountState"] = true;
                    totalPrice += (float)consumption.Price;
                }
                else
                {
                    row["CheckBoxOption"] = false;                    
                    row["Amount"] = 1;
                    row["AmountState"] = false;
                }
                
                dataTable.Rows.Add(row);
            }

            view.total_cost = totalPrice;
            view.options = dataTable;
        }

        public void FillOptions(int orderId)
        {
            DetailService service = new DetailService();

            IList<Detail> listDetails = service.GetAllDetailsOfAnOrder(orderId);

            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("OptionID");
            dataTable.Columns.Add("CheckBoxOption" , typeof(bool));
            dataTable.Columns.Add("Description");
            dataTable.Columns.Add("Price");
            dataTable.Columns.Add("Amount");

            foreach(var detail in listDetails){

                DataRow row = dataTable.NewRow();
                row["OptionID"] = detail.DetailId;
                row["CheckBoxOption"] = false;
                row["Description"] = detail.Product.Name;
                row["Price"] = detail.Product.Price;
                row["Amount"] = 1;
                dataTable.Rows.Add(row);
            }

            view.options = dataTable;

        }

        public void AddConsumption()
        {
            ConsumptionService service = new ConsumptionService();

            List<Consumption> listConsumption = new List<Consumption>();

            foreach(DataRow row in view.options.Rows){

                Consumption consumption = new Consumption();

                consumption.UserId = view.user_id;
                consumption.UserName = view.user_name;
                int detailId = int.Parse(row["OptionID"].ToString());
                int amount = int.Parse(row["Amount"].ToString());
                float price = float.Parse(row["Price"].ToString());

                consumption.Quantity = amount;
                consumption.Price = price * amount;
                
                service.AddConsumptionByOrder(detailId, consumption);

            }

        }

        public void EditConsumption()
        {
            DetailService detailService = new DetailService();
            ConsumptionService consumptionService = new ConsumptionService();

            IList<Detail> listDetails = detailService.GetAllDetailsOfAnOrder(view.orderId);            

            foreach (var detail in listDetails)
            {
                Consumption consumption = consumptionService.GetConsumptionForDetailAndUserID(detail.DetailId, view.user_id);
                if (consumption != null)
                {
                    consumptionService.DeleteConsumption(consumption);    
                }                
            }
            
            AddConsumption();
        }
    }
}
